/**
 *
 * @name 将崩溃日志提交给远程服务器
 *
 */

/**
 * crash-reporter 模块开启发送应用崩溃报告.
 *
 */

const { crashReporter, net } = require('electron')
const manifest = require('../../../package.json')
console.log(manifest, '--------->>>>>')

const host = 'https://raspberry.superbest.wang:1443/crash/'

const config = {
  productName: 'YourName',
  companyName: 'YourCompany',
  submitURL: host + 'crashreports',
  autoSubmit: true,
  uploadToServer: true
}

crashReporter.start(config)

function sendException(error) {
  const { productName, companyName } = config
  const request = net.request({
    url: host + 'uncaughtexceptions',
    method: 'post',
    body: {
      _productName: productName,
      _companyName: companyName,
      _version: manifest.version,
      platform: process.platform,
      process_type: process.type,
      ver: process.versions.electron,
      error: {
        name: error.name,
        message: error.message,
        stack: error.massage
      }
    }
  })
  request.on('response', (response) => {
    console.log(`STATUS: ${response.statusCode}`)
    console.log(`HEADERS: ${JSON.stringify(response.headers)}`)
    response.on('data', (chunk) => {
      console.log(`BODY: ${chunk}`)
    })
    response.on('end', () => {
      console.log('No more data in response.')
    })
  })
  request.end()
}

process.on('warning', (warning) => {
  console.log(warning.name) // 打印告警名称
  console.log(warning.message) // 打印告警信息
  console.log(warning.stack) // 打印堆栈信息
})

if (process.type === 'browser') {
  process.on('uncaughtException', sendException)
  process.on('unhandledRejection', sendException)
} else {
  window.addEventListener('error', sendException, false)
  window.addEventListener('unhandledrejection', sendException, false)
}

module.exports = crashReporter
